package com.jqq.threaduse.method;

/**
 * @author 姬倩倩
 * @version 1.0
 * 主线程每隔1s输出hi，共10次，当输出到hi5时，启动子线程，每隔1s输出hello,
 * 输出10次后退出，主线程继续输出hi,直到主线程退出
 */
public class ThreadMethodExercise {
    public static void main(String[] args) throws InterruptedException {
        T3 t3 = new T3();
        Thread thread = new Thread(t3);
        for (int i = 1; i <= 10; i++) {
            System.out.println("hi" + i);

            if (i == 5){
                thread.start();//启动子线程，输出hello..
                thread.join();//立即将t3子线程，插入到main线程，让t3先执行
            }
            Thread.sleep(1000);//主线程休眠1s
        }
        System.out.println("主线程结束..");
    }
}

class T3 implements Runnable {
    @Override
    public void run() {
        for (int i = 1; i <= 10; i++) {
            System.out.println("hello" + i);
            try {
                Thread.sleep(1000);//休眠1s
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("子线程结束..");
    }
}